package com.yang.biz.modular.taskcenter.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.biz.modular.taskcenter.entity.Task;
import com.yang.biz.modular.taskcenter.enums.TaskStatusEnum;
import com.yang.biz.modular.taskcenter.mapper.TaskMapper;
import com.yang.biz.modular.taskcenter.param.TaskAddParam;
import com.yang.biz.modular.taskcenter.param.TaskPageParam;
import com.yang.biz.modular.taskcenter.param.TaskUpdateParam;
import com.yang.biz.modular.taskcenter.result.TaskResult;
import com.yang.biz.modular.taskcenter.service.TaskService;
import com.yang.common.enums.CommonSortOrderEnum;
import com.yang.common.exception.CommonException;
import com.yang.common.page.CommonPageRequest;
import org.springframework.stereotype.Service;

/**
 * @author: yangjianzhi
 * @version1.0
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

    @Override
    public JSONObject add(TaskAddParam taskAddParam) {
        TaskResult taskResult = BeanUtil.toBean(taskAddParam, TaskResult.class);
        Task task = TaskResult.voToObj(taskResult);
        String description = task.getDescription();
        String status = task.getStatus();

        if (!(StrUtil.isAllNotBlank(description, status) && TaskStatusEnum.valid(status))) {
            return JSONUtil.createObj().set("code", 0).set("msg", "参数不规范!");
        }
        boolean save = this.save(task);
        return JSONUtil.createObj()
                .set("code", save ? 1 : 0)
                .set("task", task);
    }

    @Override
    public JSONObject update(TaskUpdateParam taskUpdateParam) {
        Task task = this.queryEntity(taskUpdateParam.getId());
        Task param = TaskResult.voToObj(BeanUtil.toBean(taskUpdateParam, TaskResult.class));
        BeanUtil.copyProperties(param, task);
        this.updateById(task);
        return JSONUtil.createObj().set("code", 1);
    }

    @Override
    public Task queryEntity(String id) {
        Task task = this.getById(id);
        if (ObjectUtil.isEmpty(task)) {
            throw new CommonException("任务不存在，id值为：{}", id);
        }
        return task;
    }

    @Override
    public Page<Task> page(TaskPageParam taskPageParam) {
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isAllNotEmpty(taskPageParam.getSortField(), taskPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(taskPageParam.getSortOrder());
            queryWrapper.orderBy(true, taskPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(taskPageParam.getSortField()));
        }
        // 条件
        queryWrapper.lambda().eq(StrUtil.isNotBlank(taskPageParam.getCategory()), Task::getCategory, taskPageParam.getCategory());
        queryWrapper.lambda().eq(StrUtil.isNotBlank(taskPageParam.getDescription()), Task::getDescription, taskPageParam.getDescription());
        queryWrapper.lambda().eq(StrUtil.isNotBlank(taskPageParam.getStatus()), Task::getStatus, taskPageParam.getStatus());
        queryWrapper.lambda().in(ObjectUtil.isNotEmpty(taskPageParam.getDescriptionList()), Task::getDescription, taskPageParam.getDescriptionList());
        queryWrapper.lambda().in(ObjectUtil.isNotEmpty(taskPageParam.getStatusList()), Task::getStatus, taskPageParam.getStatusList());
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }
}
